package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Region;
import cn.wolfcode.wolf2w.queryObject.DestinationQueryObject;
import cn.wolfcode.wolf2w.repository.DestinationRepository;
import cn.wolfcode.wolf2w.repository.RegionRepository;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IRegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.expression.spel.ast.NullLiteral;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.*;

@Service
public class DestinationServiceImpl implements IDestinationService {

    @Autowired
    private DestinationRepository destinationRepository;
    @Autowired
    private MongoTemplate template;
    @Autowired
    private IRegionService regionService;

    @Override
    public void save(Destination destination) {
        destinationRepository.save(destination);
    }

    @Override
    public void update(Destination destination) {
        destinationRepository.save(destination);
    }

    @Override
    public void delete(String id) {
        destinationRepository.deleteById(id);
    }

    @Override
    public Destination get(String id) {
        Optional<Destination> destination = destinationRepository.findById(id);
        return destination.orElse(null);
    }

    @Override
    public List<Destination> list() {
        List<Destination> all = destinationRepository.findAll();
        return all;
    }

    /**
     * 目的地分页
     * @param qo
     * @return
     */
    @Override
    public Page<Destination> query(DestinationQueryObject qo) {

        // 封装查询条件
        Query query = new Query();
        // 如果有关键字高查就拼接关键字
        if (StringUtils.hasLength(qo.getKeyword())){
            query.addCriteria(Criteria.where("name").regex(qo.getKeyword()));

        }
        if (StringUtils.hasLength(qo.getParentId())){
            query.addCriteria(Criteria.where("parentId").is(qo.getParentId()));
        }/*else {
            query.addCriteria(Criteria.where("parentId").is(null));
        }*/


        // 总条数
        long totalCount = template.count(query, Destination.class);

        // 如果总条数为0 , 那么返回空的分页对象
        if (totalCount==0){
            return Page.empty();
        }

        // 分页对象 , 参数1为当前页 从0开始 , 参数2为每页显示条数 , 参数3为排序方式, 参数4为按哪个字段排序
        Pageable pageable = PageRequest.of(qo.getCurrentPage()-1, qo.getPageSize(),
                Sort.Direction.ASC,"id");

        query.with(pageable);// 添加查询条件

        List<Destination> list = template.find(query, Destination.class);

        // PageImpl 为 Page 接口的一个实现类
        return new PageImpl<Destination>(list, pageable,totalCount );
    }


    /**
     * 获取吐司list
     * @param pId 父id
     * @return
     */
    @Override
    public List<Destination> getToasts(String pId) {

        List<Destination> destinations = new ArrayList<>();

        String parentId = pId;

        // 上级id不能为空
        while (StringUtils.hasLength(parentId)){
            Destination destination = this.get(parentId);
            parentId = destination.getParentId();
            destinations.add(destination);
        }

        // 反转list
        Collections.reverse(destinations);

        return destinations;
    }

    @Override
    public List<Destination> queryDestByRegionIdForApi(String regionId) {

        List<Destination> list = null;

        if ("-1".equals(regionId)){
            // 如果传过来的是-1表示是国内的
            list = destinationRepository.findByParentName("中国");
        }else {
            list = regionService.getDestByRegionId(regionId);
        }

        // 通过热门目的地的id去找到以它作为parentId的子目的地
        // 把查出来的子目的地列表放入目的地的children字段中
        for (Destination destination : list) {
            List<Destination> children = destinationRepository.findByParentId(destination.getId());
            destination.setChildren(children);
        }

        if (list.size()>5){
            List<Destination> listD = list.subList(0, 5);
            return listD;
        }

        return list;
    }

    @Override
    public void updateInfo(String id, String info) {

        Destination destination = destinationRepository.findById(id).orElse(null);

        destination.setInfo(info);

        destinationRepository.save(destination);

    }


}
